package com.corewillsoft.fireflies.social;

import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

import com.facebook.FacebookRequestError;
import com.facebook.HttpMethod;
import com.facebook.Request;
import com.facebook.RequestAsyncTask;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.model.GraphUser;

public class FacebookManager {

	private final static String TAG = "FacebookManager";

	private static final String MY_APP_ID = "236063063084269";
//	"publish_actions",r
	private final List<String> PERMISSIONS = Arrays.asList( "user_birthday",
			"user_relationship_details", "email", "user_photos");

	private Activity activity;

	public static FacebookManager getInstance(Activity activity) {
		return new FacebookManager(activity);
	}

	private FacebookManager(Activity activity) {
		this.activity = activity;
	}

	public void publishStory() {
		initSession();
		if (!Session.getActiveSession().isOpened()) {
			Toast.makeText(getActivity(), "session is close", Toast.LENGTH_SHORT).show();
			return;
		}
		// postData();
		requestUserInfo();
	}

	private Activity getActivity() {
		return activity;
	}

	private void initSession() {
		Session session = Session.getActiveSession();
		if (session == null) {
			session = new Session(activity);
			Session.OpenRequest openRequest = new Session.OpenRequest(activity);
			openRequest.setPermissions(PERMISSIONS);
			session.openForRead(openRequest);
			Session.setActiveSession(session);
		}
	}

	private void postData() {
		Session session = Session.getActiveSession();

		if (session != null) {

			Bundle postParams = new Bundle();
			postParams.putString("name", "Facebook SDK for Android");
			postParams.putString("caption", "Build great social apps and get more installs.");
			postParams
					.putString("description",
							"The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps.");
			postParams.putString("link", "https://developers.facebook.com/android");
			postParams.putString("picture",
					"https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");

			Request.Callback callback = new Request.Callback() {

				public void onCompleted(Response response) {
					FacebookRequestError error = response.getError();
					if (error != null) {
						Toast.makeText(getActivity()
								.getApplicationContext(),
								error.getErrorMessage(),
								Toast.LENGTH_SHORT).show();
					} else {
						Toast.makeText(getActivity()
								.getApplicationContext(),
								"success",
								Toast.LENGTH_LONG).show();
					}
				}
			};

			Request request = new Request(session, "me/feed", postParams,
					HttpMethod.POST, callback);

			RequestAsyncTask task = new RequestAsyncTask(request);
			task.execute();
		}
	}

	private void requestUserInfo() {
		Session session = Session.getActiveSession();
		Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

			@Override
			public void onCompleted(GraphUser user, Response response) {
				FacebookRequestError error = response.getError();
				if (error != null) {
					Toast.makeText(getActivity()
							.getApplicationContext(),
							error.getErrorMessage(),
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(getActivity()
							.getApplicationContext(),
							"NAME = " + user.getName(),
							Toast.LENGTH_LONG).show();
				}
			}
		});
	}
}